﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TireBias.Models;
using WinNemoLib;

namespace TireBias.Controllers
{
    public class MatToMatController : Controller
    {
        //
        // GET: /MatToMat/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult matTomat()
        {
            ViewBag.Full = "F";
            return View();
        }
        public JsonResult Get_barcode(string seq, string rollno, string ply, string biasno)
        {
            //SessionBag.Current.biasno = 1;
            
            TailModel md = new TailModel();
            string[] seqs = seq.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);
            List<TailModel.PlyRoll_DS> data = new List<TailModel.PlyRoll_DS>();
            data = md.PRINT_PLY(biasno, seqs[0], seqs[1], seqs[2], ply, rollno);

            return Json(data);     
        }

        public JsonResult SAVE_MAT(List<TailModel.PlyRoll_DS> theData)
        {
            if (theData == null)
            {
                return Json(new { success = 0, message = "ไม่สามารถบันทึกได้" });
            }
            TailModel mdUpd = new TailModel();
            using (ConnectionWrapper cnn = Nemo.GetDefaultConfiguredDbConnectionWrapper())
            {
                cnn.Open();
                cnn.BeginTransaction();

                foreach (TailModel.PlyRoll_DS data in theData)
                {
                    data._empname = User.Identity.Name;

                    if (!mdUpd.DEL_MatToMat(cnn, data._biasno, data._plandt, data._itemno, data._sitemno, data._side, data._rollno))
                    {
                        cnn.RollbackTransaction();
                        return Json(new { success = 0, message = "ไม่สามารถบันทึกได้" });
                    }
                    if (!mdUpd.Ins_MatToMat(cnn, data))
                    {
                        cnn.RollbackTransaction();
                        return Json(new { success = 0, message = "ไม่สามารถบันทึกได้" });
                    }
                }
                cnn.CommitTransaction();
                return Json(new { success = 1, message = "บันทึกเรียบร้อย" });
            }
        }
    }
}
